﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerBooking.RSE.Domain.Models;
using PowerBooking.RSE.Domain.Factories;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Threading.Tasks;
using PowerBooking.RSE.Repository;
using PowerBooking.RSE.Utility.Helpers;
using System.Collections.Concurrent;
using System.Transactions;

namespace PowerBooking.RSE.Domain.Services
{
    /// <summary>
    /// 餐厅蜘蛛引擎服务
    /// </summary>
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class RestaurantSpiderEngineService
    {
        /// <summary>
        /// 根据蜘蛛ID捕获餐厅
        /// </summary>
        [WebGet(UriTemplate = "CaptureRestaurantBySpiderID")]
        public void CaptureRestaurantBySpiderID(int spiderID)
        {
            CacheHelper.Set("IsCaptureStop", false, DateTime.Now.AddDays(1));

            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    var context = new UnitOfWork();

                    var spiderFactory = new SpiderFactory(context);
                    var cityFactory = new CityFactory(context);
                    var filterFactory = new FilterFactory(context);
                    var restaurantFactory = new RestaurantFactory(context);

                    var spider = spiderFactory.GetSpiderBySpiderID(spiderID);
                    spider.InitCities(cityFactory.CreateXiaomishuCities(spiderID, spider.Url));
                    Parallel.For(0, spider.Cities.Count, (c) =>
                    {
                        var restaurants = new List<Restaurant>();
                        spider.Cities[c].InitFilters(filterFactory.CreateXiaomishuFilters(spider.Cities[c].CityID, spider.Cities[c].Url));
                        Parallel.For(0, spider.Cities[c].Filters.Count, (f) =>
                        {
                            if (!CacheHelper.Get<bool>("IsCaptureStop"))
                            {
                                restaurants.AddRange(restaurantFactory.CreateXiaomishuRestaurants(spider.Cities[c].CityID, spider.Cities[c].Filters[f].FilterID, spider.Cities[c].Url, spider.Cities[c].Filters[f].Url));
                            }
                            else
                            {
                                return;
                            }
                        });


                        spider.Cities[c].InitRestaurants(restaurants);
                    });

                    context.AcceptAllChanges();
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    transactionScope.Complete();
                }
            }
        }

        /// <summary>
        /// 捕获所有餐厅
        /// </summary>
        [WebGet(UriTemplate = "CaptureAllRestaurants")]
        public void CaptureALlRestaurants()
        {
            CacheHelper.Set("IsCaptureStop", false, DateTime.Now.AddDays(1));
            var dbContext = new RestaurantSpiderEngineContext();
            var spiderFactory = new SpiderFactory(dbContext);
            var spiders = spiderFactory.CreateSpiders();
        }

        /// <summary>
        /// 停止捕获
        /// </summary>
        [WebGet(UriTemplate = "StopCapture")]
        public void StopCapture()
        {
            CacheHelper.Set("IsCaptureStop", true, DateTime.Now.AddDays(1));
        }
    }
}
